/**
 * function myChartListener(event, parameter){ alert( event.sender.id + " has
 * completed chart drawing" ); } CnbiSoft("myChartId").addEventListener
 * ("DrawComplete" , myChartListener );
 */
define(['apps/utils/tools', 'apps/utils/math'], function(tools, math) {
	/**
	 * 图默认的属性
	 */
	 var defaults = {
		    styles:{
					definition : [{
								name : "captionStyle",
								font : "微软雅黑",
								size : 13,
								bgColor : "FFFFDD",
								color : "000"
							}],
					application : [{
								toobject : "caption",
								styles : "captionStyle"
							}]
		 }, chart:{
			    bgcolor : "FFFFFF",
				palette : 2,
				showvalues : 1,
				showborder : 1,
				formatnumber : 1,
				formatnumberscale : 0,
				forcedecimals : 1,
				decimals:2,
				borderColor:'dddddd'
			}
	   };
	   function demoLine(){
			var chartData={
					  "chart": {
					    "palette": "2",
					    "caption": "Sales Comparison",
					    "showvalues": "0",
					    "numvdivlines": "10",
					    "drawanchors": "0",
					    "numberprefix": "$",
					    "divlinealpha": "30",
					    "alternatehgridalpha": "20",
					    "setadaptiveymin": "1"
					  },
					  "styles": {
					    "definition": [
					      {
					        "name": "XScaleAnim",
					        "type": "ANIMATION",
					        "duration": "0.5",
					        "start": "0",
					        "param": "_xScale"
					      },
					      {
					        "name": "YScaleAnim",
					        "type": "ANIMATION",
					        "duration": "0.5",
					        "start": "0",
					        "param": "_yscale"
					      },
					      {
					        "name": "XAnim",
					        "type": "ANIMATION",
					        "duration": "0.5",
					        "start": "0",
					        "param": "_yscale"
					      },
					      {
					        "name": "AlphaAnim",
					        "type": "ANIMATION",
					        "duration": "0.5",
					        "start": "0",
					        "param": "_alpha"
					      }
					    ],
					    "application": [
					      {
					        "toobject": "CANVAS",
					        "styles": "XScaleAnim, YScaleAnim,AlphaAnim"
					      },
					      {
					        "toobject": "DIVLINES",
					        "styles": "XScaleAnim,AlphaAnim"
					      },
					      {
					        "toobject": "VDIVLINES",
					        "styles": "YScaleAnim,AlphaAnim"
					      },
					      {
					        "toobject": "HGRID",
					        "styles": "YScaleAnim,AlphaAnim"
					      }
					    ]
					  }
}
	   }
	    /**
		 * 获取单值
		 */
	   function getSingleDataChart(bean) {
			if(bean["chart"] )tools.applyIf(defaults["chart"],bean["chart"]);
			if(bean["styles"])tools.applyIf(defaults["styles"],bean["styles"]);
			// chart:defaults["chart"],styles:defaults["styles"],
			var result ={chart:bean["chart"],colorrange:bean["colorrange"],dials:bean["dials"]};
			if(bean["annotations"]){
			 result["annotations"] = bean["annotations"];
			}
			 return result;

		}
	   /**
		 * 获取单系列图的数据pie column 金字塔 bar等
		 */
	   function getSingleSeriesData(bean) {
			 var data = [];
				if(!bean.data){
				for(var i=0 ,len = bean["value"].length;i<len;i++){
				    data.push({label:bean["label"][i],value:bean["value"][i],tooltext:bean["label"][i]+bean["value"][i]});
				}
			   }else{
			      data = bean.data;
			   }
			if(bean["chart"]){
			    if(bean.swf == "Radar"){
			       defaults["chart"] = bean["chart"]
			    }else{
			    tools.applyIf(defaults["chart"],bean["chart"]);
			    }
			}
			
			if(bean["styles"])tools.applyIf(defaults["styles"],bean["styles"]);
			return {chart:defaults["chart"],styles:defaults["styles"],data:data};
		}
function getMuliSeriesData(bean) {
				var dataset = [];
				var categories = [];
				categories.push({"category":bean.category});
				tools.applyIf(dataset,bean.dataset);
//				for(var oo=0,ll = dataset.length;oo<ll;oo++){
//	      	          for(var i=0,len = dataset[oo]["data"].length;i<len;i++){
//	      	             if(dataset[oo]["data"][i]["vars"]){
//	      	                delete dataset[oo]["data"][i].vars;
//	      	             }
//	      	          }
//	      	       }
				//if(bean["chart"])tools.applyIf(defaults.chart,bean["chart"]);
				if(bean["styles"])tools.applyIf(defaults.styles,bean["styles"]);
				return {chart:bean["chart"],styles:defaults.styles,categories:categories,dataset:dataset};

			}
			
			function getChart(bean){
				if(!bean["chartId"]){
				   bean["chartId"]= bean["swf"] + "chartId", myChart = {};
				}
				if (CnbiSoft(bean["chartId"])) {
					myChart = CnbiSoft(bean["chartId"]);
				} else {
					myChart = new CnbiSoft(tools.getBasePathUrl() + "resources/charts/swf/"+ bean["swf"] + ".swf", bean["chartId"], "100%",bean["height"]?bean["height"]:"100%","1");
				}
				return myChart;
	     }
	  function renderBefore(){
	       CnbiSoft.setCurrentRenderer('JavaScript');
	  }
	  function createSingleDataChart(bean){
	  	   renderBefore();
	  	      var chartData = getSingleDataChart(bean),
	          myChart = getChart(bean);
			  myChart.setJSONData(chartData);// chart.view2D();
			  myChart.render(bean["domId"]);
	  }
		/**
		 * 创建单系列图如：pie column 金字塔 bar等
		 */	
	 function createSingleSeriesChart(bean) {
			  renderBefore();
			 var chartData =  getSingleSeriesData(bean),
			 myChart = getChart(bean);
			 myChart.setJSONData(chartData)
			 myChart.render(bean["domId"]);
		}
	   function setChartAttribute(bean,obj){
		   getChart(bean).setChartAttribute(obj.key,obj.value );
		}
	    function createMultiSeriesChart(bean){
				renderBefore();
				var chartData={};
				if(!bean.flag){
				   chartData = getMuliSeriesData(bean);
				}else{
				  chartData = bean.data;
				  //if(bean.data["chart"])tools.applyIf(bean.data["chart"],);
				}
				myChart = getChart(bean);
				myChart.setJSONData(chartData);// chart.view2D();
				myChart.render(bean["domId"]);
			};
			function getChartAttr(bean){
				return $("#"+bean["domId"]).attrCnbiSofts(bean.attr)
			}
			function changeChartTheme(bean){// D977B7
			   $("#"+bean["domId"]).attrCnbiSofts({"palette": bean["palette"], "paletteColors": bean["paletteColors"]});
			}
			function updateChart(bean){
				if(!bean["types"]) bean["types"] = "json";
				var datas ={};
				if(bean.type == 2){
				  datas =getSingleSeriesData(bean);
				}else{
				   datas =getMuliSeriesData(bean);
				}
			     $("#"+bean["domId"]).updateCnbiSoft({dataSource:datas, dataFormat:bean["types"]});
			}
			
			/**
			 * Print Page
			 */
			function printPage (){
			    CnbiSoft.printManager.managedPrint()
			}
			
			return {
				getSingleSeriesData : getSingleSeriesData,
				createSingleSeriesChart:createSingleSeriesChart,
				createSingleDataChart:createSingleDataChart,
				getChart:getChart,
				updateChart:updateChart,
				printPage:printPage,
				setChartAttribute:setChartAttribute,
				printPage:printPage,
				createMultiSeriesChart:createMultiSeriesChart
			};

		});